//
//  JYShoppingCartManager.m
//  JYFarm
//
//  Created by henry on 2017/11/2.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYShoppingCartManager.h"
#import "JYShopModel.h"

@interface JYShoppingCartManager()
//添加的商品列表
@property (nonatomic,strong) NSMutableArray *localGoodsList;

@end

@implementation JYShoppingCartManager

#pragma mark - 单例 -
+ (instancetype)shareInstance{
    static dispatch_once_t onceToken;
    static JYShoppingCartManager *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [JYShoppingCartManager new];
        
        
    });
    return instance;
}
#pragma mark - 本地操作 -
- (void)deleteGoods:(JYShopModel *)model{
    [self deleteGoodsList:@[model]];
}


/**
 批量删除商品
 
 @param goodsLost 商品数组
 */
- (void)deleteGoodsList:(NSArray <JYShopModel*>*)goodsLost{
    for (JYShopModel *model in goodsLost) {
        [self modifyLocalGoodsListGoods:model];
    }
}

/**
 添加某个商品
 
 @param model 商品模型
 */
- (void)addGoods:(JYShopModel *)model{
    [self addGoodsList:@[model]];
}


- (void)addGoodsList:(NSArray <JYShopModel*>*)goodsLost{
    for (JYShopModel *model in goodsLost) {
        [self modifyLocalGoodsListGoods:model];
    }
}


- (void)synchronize:(void(^)(BOOL succ))completeBlock{
    //网络请求
#warning 填充代码
}


/**
 修改本地商品数组
 */
- (void)modifyLocalGoodsListGoods:(JYShopModel *)model{
    //对_localGoodsList进行操作
    JYShopModel *newShopModel = ({
#warning coding 判断 _localGoodsList是否包含该商品，包含则返回该模型，不然则返回nil
        nil;
    });
    
    if(newShopModel == nil){
#warning 将model加入到_localGoodsList中
        return;
    }

#warning 对newShipModel的数量进行调节
}

#pragma mark - 同步操作 -
- (void)syncDeleteGoods:(JYShopModel *)model completecompleteBlock:(void(^)(BOOL succ))completeBlock{
    [self syncDeleteGoodsList:@[model] completecompleteBlock:completeBlock];
}
//批量删除商品
- (void)syncDeleteGoodsList:(NSArray <JYShopModel*>*)goodsLost completecompleteBlock:(void(^)(BOOL succ))completeBlock{
#warning 网络请求将商品从购物车移除
    
    
    NSMutableArray *goodListM = [NSMutableArray arrayWithArray:goodsLost];
    NSMutableArray *selecetdArr = [NSMutableArray array];
    NSMutableArray *listArr = [NSMutableArray array];
    for (JYShopModel *model in goodListM) {
        
        if (model.selected) {
            [selecetdArr addObject:model];
        }
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"goodsId"] = model.goodsId;
        dic[@"userId"] = LH.userId;
        dic[@"categroyId"] = model.categroyId;
        [listArr addObject:dic];
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"deletShoppingTrolleyType"] = @(2);
    param[@"userId"] = LH.userId;
    [param setValue:listArr forKey:@"list"];
    
    [HTTPRequest POST:kDeleteuserShoppingTrolleyUrl parameter:param success:^(id resposeObject) {
        
        if (SUCCESS) {
            [goodListM removeObjectsInArray:selecetdArr];
            
            if (completeBlock) {
                completeBlock(YES);
            }
        }else{
            
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
    }];

}

- (void)syncAddGoods:(JYShopModel *)model completecompleteBlock:(void(^)(BOOL succ))completeBlock{
    [self syncAddGoodsList:@[model] completecompleteBlock:completeBlock];
}


- (void)syncAddGoodsList:(NSArray <JYShopModel*>*)goodsLost completecompleteBlock:(void(^)(BOOL succ))completeBlock{
#warning 网络请求添加商品到购物车



    for (JYShopModel *model in goodsLost) {
        
        
    }

}


/**
 刷新购物车
 
 @param completeBlock 请求完成回调
 */
- (void)refreshShoppingCart:(void(^)(JYShoppingCartManager *shoppingCartMgr,NSError *error))completeBlock{
#warning 网络请求
    
    [HTTPRequest POST:kUserShoppingTrolleyListUrl parameter:@{@"userId":LH.userId} success:^(id resposeObject) {
#warning 数据整理，将商品数组赋值JYShoppingCartManager的goodsList
#warning 然后回调completeBlock方法
        //if(completeBlock){
        //    completeBlock(self,nil);
        //}
    } failure:^(NSError *error) {
        if(completeBlock){
            completeBlock(self,error);
        }
    }];
    
}


- (void)cleanShoppingCart:(void(^)(BOOL succ))completeBlock{
#warning  清空购物车
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"userId"] = LH.userId;
    params[@"deletShoppingTrolleyType"] = @"1";
    
    [HTTPRequest POST:kDeleteuserShoppingTrolleyUrl parameter:params success:^(id resposeObject) {
        
        if (SUCCESS) {
            self.goodsList = nil;
            if (completeBlock) {
                completeBlock(YES);
            }
        }else{
            
     [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
            
        }
    } failure:^(NSError *error) {
        
     [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
    }];
}


@end
